package cn.codewen.shop.servlet;

import cn.codewen.shop.pojo.Category;
import cn.codewen.shop.pojo.Product;
import cn.codewen.shop.service.impl.CategoryServiceImpl;
import cn.codewen.shop.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index/*")
public class IndexServlet extends BaseServlet {

    public void main(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //1. 接收数据
        //2. 处理数据
        //2.1 得到所有分类
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        List<Category> categories = categoryService.findAll();
        //2.2 得到所有商品
        ProductServiceImpl productService = new ProductServiceImpl();
        List<Product> products = productService.findAll();
        //3. 转发
        //3.1 转发数据
        request.setAttribute("categories",categories);
        request.setAttribute("products",products);
        //3.2 重定向
        request.getRequestDispatcher("/main.jsp").forward(request,response);


    }
    public void detail(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //1. 接收数据
        int id = Integer.parseInt(request.getParameter("id"));
        //2. 处理数据
        //2.1 得到当前商品详情
        ProductServiceImpl productService = new ProductServiceImpl();
        Product product = productService.getOne(id);
        //3. 转发
        //3.1 转发数据
        request.setAttribute("product",product);
        //3.2 重定向
        request.getRequestDispatcher("/detail.jsp").forward(request,response);


    }

}
